home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3819 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: prairie.nodak.edu!not-for-mail
  2. From: wstark@prairie.nodak.edu (Just Me)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: why get 9 strings only?
  5. Date: 31 Jan 1996 05:56:48 -0600
  6. Organization: North Dakota Higher Education Computing Network (NDHECN)
  7. Message-ID: <4enle0$hfs@prairie.nodak.edu>
  8. References: <4e51th$4bi@news.nevada.edu> <310909EC.782B007C@eiffel.com>
  9. NNTP-Posting-Host: prairie.nodak.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=US-ASCII
  12. Content-Transfer-Encoding: 7bit
  13.  
  14. In article <310909EC.782B007C@eiffel.com>,
  15. Guus Leeuw jr. <guusl@eiffel.com> wrote:
  16.  
  17. I realize that this is just a simple program, but there are some concepts
  18. that should be pointed out, anyway.
  19.  
  20. >int
  21. >main ()
  22. >{
  23. >  char str[10][80];
  24. >  char dummy[2];    /* *** new new new *** */
  25.  
  26. why make it just 2? are you sure that's big enough? (see below)
  27.  
  28. >  int i, j;
  29.  
  30. >  printf ("Enter a number (0-9):\n");
  31. >  scanf ("%d", &j);
  32.  
  33. scanf() is not meant for processing raw data from a uncontrollable source,
  34. in this case, it's not a mortal sin, but it still isn't all that great.  A
  35. better method would be to read a complete line into an appropriate buffer
  36. with fgets() (NOT gets()), and process it with either atoi(), or sscanf().
  37.  
  38. Then, check to make sure the whole line fit in the buffer (i.e. was there a
  39. newline preceding the '\0' in the buffer), and possibly eat the remaining
  40. bit of the line with getchar() in a loop.
  41.  
  42. >  gets (dummy);        /* *** Get the empty string *** */
  43.  
  44. gets() doesn't check buffer sizes, to see a possibly spectacular failure,
  45. just enter "3<with a lot of giberish following the number>\n" at the prompt
  46. printed above.  I'm not responsible if what you type happens to cause damage
  47. :-)
  48.  
  49. >  printf ("Enter 10 strings:\n");
  50. >  for (i = 0; i < 10; i++)  /* get 10 strings */
  51. >    gets (str[i]);
  52.  
  53. see comment above about gets(), however you must be aware that if one line
  54. was too long, the tail of it will be read on the next iteration.  You have
  55. to handle a bit more when using fgets(), but it's much better than
  56. overwriting random parts of your program.  This type of error wouldn't be
  57. caught in most memory protected systems, since the program is still
  58. accessing memory inside its address space (unless you type a truly excessive
  59. amount of gibberish).
  60.  
  61. >  if (j >= 0 && j < 10)        /* *** changed *** */
  62. >    printf ("Answer: %s\n", str[j]);
  63.  
  64. >  return 1;
  65.  
  66. it would be more appropriate to return 0, (or EXIT_SUCCESS), instead of 1.
  67.  
  68. >}
  69.